﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace CarBlackBox
{
    public partial class Form_VehicleManage : Form
    {
        public Form_VehicleManage()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form_VehicleEditor f = new Form_VehicleEditor(this);
            f.ShowDialog();
//             if(f.ShowDialog()==DialogResult.OK)
//                 this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
        }
        private delegate void renovateVehicleInfo();
        public void RenovateVehicleInfo()
        {
            this.Invoke(new renovateVehicleInfo(delegate() { this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo); }));
        }
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
                return;
            VehicleInfoData Vehicle;
            int index = dataGridView1.SelectedRows[0].Index;
            Vehicle = VehicleInfoDataFactory.Construct(dataGridView1, index);
            Form_SetRecorder f = new Form_SetRecorder(Vehicle);
            f.ShowDialog();            
        }

        private void Form_VehicleManage_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“systemDBDataSet.VehicleInfo”中。您可以根据需要移动或移除它。
            //this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("全部", 0));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("车牌号码", 1));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("车辆识别号VIN", 2));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("车牌类型", 3));
            toolStripComboBox1.Items.Add(new WFNetLib.MyControls.ComboBoxItem("记录仪编号", 4));
            toolStripComboBox1.SelectedIndex = 0;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
                return;
            if (WFNetLib.WFGlobal.MessageYseOrNo("确认删除当前的选定项么？", "删除") == DialogResult.No)
                return;
            string LicensePlate = VehicleInfoDataFactory.GetPrimaryKey(dataGridView1, dataGridView1.SelectedRows[0].Index);
            //dataGridView1.SelectedRows[0].Cells["车牌号DataGridViewTextBoxColumn"].Value.ToString();dataGridView1.SelectedRows[0].Index
            if(vehicleInfoTableAdapter.DeleteQuery(LicensePlate)==1)
            {
                MessageBox.Show("删除成功");
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.车辆资料管理.ToString();
                log.LogContent = "删除车牌号为" + LicensePlate + "的车辆资料";
                log.LogRemark = "车辆资料删除";
                log.InserSQLLog();
                this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);  
            }
            else
            {
                MessageBox.Show("删除中出现某些问题，请联系管理员");
            }          
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.RowIndex>=0)//左键双击
            {
                VehicleInfoData Vehicle;
                Vehicle = VehicleInfoDataFactory.Construct(dataGridView1, e.RowIndex);
                Form_VehicleEditor f = new Form_VehicleEditor(Vehicle);                
                if (f.ShowDialog() == DialogResult.OK)
                {
                    this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                }                
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
                return;            
            VehicleInfoData Vehicle;
            int index=dataGridView1.SelectedRows[0].Index;
            Vehicle = VehicleInfoDataFactory.Construct(dataGridView1, index);
            Form_VehicleEditor f = new Form_VehicleEditor(Vehicle);
            if (f.ShowDialog() == DialogResult.OK)
            {
                this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
                dataGridView1.Rows[index].Selected = true;
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripComboBox2.Enabled=true;
            DataTable dt;
            toolStripComboBox2.Items.Clear();
            toolStripComboBox2.DropDownStyle = ComboBoxStyle.DropDown;
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0:
                    toolStripComboBox2.Enabled = false;
                    break;
                case 1:
                    dt = vehicleInfoTableAdapter.GetLicensePlates();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[VehicleInfoData.LICENSEPLATE_Field]);
                    }
                    break;   
                case 2:
                    dt = vehicleInfoTableAdapter.GetVINs();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[VehicleInfoData.VIN_Field]);
                    }
                    break;
                case 3:
                    dt = carTypeTableAdapter1.GetData();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(new WFNetLib.MyControls.ComboBoxItem(dr[CarTypeData.NAME_Field].ToString(),dr[CarTypeData.ID_Field]));
                    }
                    toolStripComboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
                    toolStripComboBox2.SelectedIndex = 0;
                    break;
                case 4:
                    dt = recordInfoTableAdapter1.GetData();
                    foreach (DataRow dr in dt.Rows)
                    {
                        toolStripComboBox2.Items.Add(dr[RecordInfoData.RIN_Field]);
                    }
                    break;
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0://全部
                    this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
                    break;
                case 1://车牌号码
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.vehicleInfoTableAdapter.FillByBlurLicensePlate(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.vehicleInfoTableAdapter.FillByLicensePlate(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    break;
                case 2://VIN
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.vehicleInfoTableAdapter.FillByBlurVIN(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.vehicleInfoTableAdapter.FillByVIN(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    break;
                case 3://车辆类型
                    byte ID;
                    WFNetLib.MyControls.ComboBoxItem cbi = (WFNetLib.MyControls.ComboBoxItem)toolStripComboBox2.SelectedItem;
                    ID = (byte)cbi.Value;
                    this.vehicleInfoTableAdapter.FillByCarTypeID(this.systemDBDataSet.VehicleInfo, ID);
                    break;
                case 4://RIN
                    if (模糊查询ToolStripMenuItem.Checked)
                    {
                        this.vehicleInfoTableAdapter.FillByBlurRIN(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    else
                    {
                        this.vehicleInfoTableAdapter.FillByRIN(this.systemDBDataSet.VehicleInfo, toolStripComboBox2.Text);
                    }
                    break;
            }
        }
    }
}
